﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebsiteDataModel;
using System.Text;

public partial class morning_gsrl : System.Web.UI.Page
{
    protected string changes = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ptime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            ptime.Attributes.Add("onfocus", "WdatePicker()");
            initInfo();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string titles = Request["title"];
        string ctitles = Request["ctitle"];
        WebsiteDataModel.WebsiteDataEntities de = new WebsiteDataEntities();
        if (!string.IsNullOrEmpty(titles))
        {
            string[] args = titles.Split(new char[] { ',' });            
            DateTime dt = DateTime.Parse(ptime.Text);
            
            foreach (string arg in args)
            {
                if (!string.IsNullOrEmpty(arg))
                {
                    kp_gsrl gsrl = new kp_gsrl();
                    gsrl.title = arg;
                    gsrl.ptime = dt;
                    gsrl.createdate = DateTime.Now;
                    de.kp_gsrl.AddObject(gsrl);
                }
            }
        }
        if (!string.IsNullOrEmpty(ctitles)) 
        {
            string[] ids = Request["cid"].Split(new char[] { ',' });
            string[] args = ctitles.Split(new char[] { ',' });
            if (ids.Length != args.Length) 
            {
                return;
            }
            for (int i = 0; i < ids.Length; i++) 
            {
                int id = int.Parse(ids[i]);
                var re = de.kp_gsrl.Where(a => a.id == id);
                foreach (kp_gsrl rl in re) 
                {
                    rl.title = args[i];
                    rl.createdate = DateTime.Now;
                }
            }
        }
        de.SaveChanges();
        initInfo();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        initInfo();
    }

    private void initInfo() 
    {
        StringBuilder result = new StringBuilder();
        DateTime dt = DateTime.Parse(ptime.Text);
        WebsiteDataEntities de = new WebsiteDataEntities();
        var re = de.kp_gsrl.Where(a => a.ptime == dt).OrderBy(a => a.id).Select(a => new { a.id, a.title });
        foreach (var rl in re)
        {
            result.AppendFormat("<div><input type=\"hidden\" name=\"cid\" value=\"{0}\" /><input type=\"text\" style=\"width:400px;\" name=\"ctitle\" value=\"{1}\" />　<a href='DelRl.aspx?ids={0}' onclick=\"return confirm('确定删除？')\"><img src=\"../images/bt_del.gif\" alt=\"删除\" /></a></div>", rl.id, rl.title);
        }
        changes = result.ToString();
    }
}